//头部信息
app.controller('UserIndex', function($rootScope, $scope, $http, $location, common, wechat, course){
    //设置网站标题
    common.setTitle('用户中心');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();
    //加载信息
    $scope.load_info    = function(){
        //获取用户信息
        common.getUserInfo(function(userInfo){
            $scope.user     = userInfo;
        });

        //加载上次课程
        var url     = config.prefix_url+'/Course/getLastWatch?code='+config.code;
        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                if(info.data){
                    $('.last-course').css('display', 'block');
                    $scope.lastCourse   = info.data;
                }
            }
        });
    }

    $scope.open     = function(url){
        $location.path(url);
    }

    //听课
    $scope.listen   = function(courseId, playType, chapterId){
        course.listen(playType, courseId, chapterId);
    }

    $scope.load_info();
});

//用户课程控制器
app.controller('UserCourse', function($rootScope, $scope, $http, $location, common, course, wechat){
    common.setTitle('用户课程');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    $scope.courses   = new Array();
    $scope.nPage    = 0;
    $scope.isEnd    = false;

    //加载数据
    $scope.load_data    = function(){
        var page    = $scope.nPage++;
        var url     = config.prefix_url+'/user/courses?page='+page+
            '&code='+config.code+
            '&status[]=1&status[]=3';

        $http.get(url).success(function(info){
            console.log(url);
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
            }else{
                $('.load-more').html('加载中...');
                $scope.nPage    = page;
                $scope.isEnd    = common.dealListInfo(info.data.list, $scope.courses, page);
            }
        });
        $('.load-more').html('加载更多');
    }

    //听课
    $scope.listen   = function(courseId, playType){
        course.listen(playType, courseId);
    }
    $scope.load_data();
});

//用户反馈控制器
app.controller('UserFeedback', function($rootScope, $scope, $http, $location, wechat, common){
    common.setTitle('意见反馈');
    //微信分享设置(userInfo)
    wechat.wxConfig();

    //监听反馈意见变动
    var stopWatch   = $scope.$watch('feedback', function(newV, oldV, scope){
        var dom     = $('.btn-feedback');
        if(dom.attr('disabled') && newV){
            dom.removeAttr('disabled');
        }

        if(!dom.attr('disabled') && !newV){
            dom.attr('disabled', true);
        }
    });
    
    $scope.do_feedback     = function(){
        var url     = config.prefix_url+'/User/feedback?code='+config.code;
        var data    = {
            'feedback'  : $scope.feedback,
            'mobile'    : $scope.mobile
        }

        $http.post(url, data).success(function(info){
            if(info.code == 1){
                stopWatch();
                $('.btn-feedback').attr('disabled', true);

                alert('反馈成功!');
            }else{
                common.dealError(info);
            }
        })
    }
});

//用户信息控制器
app.controller('UserInfo', function($rootScope, $scope, $http, $location, common, wechat){
    common.setTitle('个人信息');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    $scope.load_info    = function(){
        //加载用户信息
        common.getUserInfo(function(userInfo){
            $scope.user     = userInfo;
            $scope.oldUser  = JSON.stringify(userInfo);
        });
    }

    // //监听输入变动
    // var btnDom  = $('.user-info .btn-submit');
    // var stopWatch   = $scope.$watch('user', function(newV, oldV, scope){
    //     console.log(scope);
    //     if(newV && newV != $scope.user.truename && btnDom.attr('disabled')){
    //         btnDom.removeAttr('disabled');
    //     }

    //     if((!newV || newV == $scope.user.truename) && !btnDom.attr('disabled')){
    //         btnDom.attr('disabled', true);
    //     }
    // });

    $scope.save_info    = function(){
        if(JSON.stringify($scope.user) == $scope.oldUser){
            common.doAlert('未检测到变更信息!');
            return false;
        }
        var url     = config.prefix_url+'/user/changeInfo?code='+config.code;
        $http.post(url, $scope.user).success(function(info){
            if(info.code == 1){
                common.doAlert('修改成功!');
                S('user_info', null);
                // btnDom.attr('disabled', true);
                // stopWatch();
            }else{
                common.dealError(info);
            }
        })
    }

    $scope.load_info();
});

//用户积分控制器
app.controller('UserScore', function($rootScope, $scope, $http, $location, common, wechat, course){
    //变量声明
    $scope.log  = {list: new Array(), is_end: false, page: 0};
    $scope.course   = {list: new Array(), is_end: false, page: 0};

    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('我的幸福豆');
        wechat.wxConfig();
    }

    //加载数据
    $scope.load_data    = function(type){
        switch(type){
            case 'log':
                var object  = $scope.log;
                var page    = object.page + 1;
                var url     = config.prefix_url+'/User/scoreLog?code='+config.code+'&page='+page;
                var domLoadMore     = $('.score-log').find('.load-more');
            break;
            case 'course':
                var object  = $scope.course;
                var page    = object.page + 1;
                var url     = config.prefix_url+'/Course/index?code='+config.code+'&page='+page+'&type=2';
                var domLoadMore     = $('.score-shop').find('.load-more');
            break;
            default: return false; break;
        }

        if(object.is_end)   return true;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                object.page     = page;
                object.is_end   = common.dealListInfo(info.data.list, object.list, page);
                domLoadMore.html('加载更多');
            }
        });
        domLoadMore.html('加载中...');
    }

    //兑换课程
    $scope.buy_course   = function(courseId){
        var url     = config.prefix_url+'/Course/buyByScore?code='+config.code+'&course_id='+courseId;
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
            }else{
                $scope.load_data('course');
            }
        });
    }

    //去听课
    $scope.listen   = function(courseId, playType){
        course.listen(playType, courseId);
    }

    $scope.init_js();
    //加载记录
    $scope.load_data('log');
    //加载课程
    $scope.load_data('course');

});

//用户订单
app.controller('UserOrder', function($rootScope, $scope, $http, $location, common, course, wechat){
    common.setTitle('我的订单');
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    $scope.orders   = new Array();
    $scope.nPage    = 1;
    $scope.isEnd    = false;

    //加载课程订单
    $scope.load_data    = function(page, callback){
        console.log(page);
        var url     = config.prefix_url+'/User/courses?code='+config.code+
            '&page='+page;

        $http.get(url).success(function(info){
            if(info.code != 1){
                common.dealError(info);
            }else{
                var list    = info.data.list;
                try{
                    if(list.length < config.page_count){
                        $scope.isEnd    = true;
                    }else{
                        $scope.isEnd    = false;
                    }
                }catch(e){
                    $scope.isEnd    = true;
                }

                for(var k in list){
                    $scope.orders.push(list[k]);
                }
                $scope.nPage    = page;
            }
            if(callback)    callback();
        });
    }

    //加载更多
    $scope.load_more    = function(){
        $('.load-more').html('加载中...');
        $scope.load_data($scope.nPage + 1, function(){
            $('.load-more').html('加载更多');
        });
    }

    //购买课程
    $scope.buy_course   = function(courseId){
        course.buy_course(courseId, function(){
            //支付成功回掉
            $timeout(function(){
                $scope.load_data(1);
            }, 2000);
        });
    }

    $scope.load_data(1);
});

//用户二维码
app.controller('UserCode', function($rootScope, $scope, $http, $location, common, wechat){
    common.setTitle('我的二维码');
    // common.getUserInfo(function(info){
    //     $scope.user     = info;
    //     // $scope.qrCode   = JSON.parse($scope.user.qrcode);
    //     // console.log($scope.qrCode);
    // });
    //微信分享设置(隐藏菜单)
    wechat.wxConfig();

    $scope.myQrcode     = config.prefix_url+'/User/getQrCodePng?code='+config.code;
    // //加载数据
    // $scope.load_data    = function(){
    //     var url     = config.prefix_url+'/User/getQrCode?code='+config.code;
    //     $http.get(url).success(function(info){
    //         console.log(info);
    //         if(info.code != 1){
    //             common.dealError(info);
    //         }else{
    //             $scope.qrCode   = info.data.info.norm_url;
    //         }
    //     });
    // }


    // //加载数据
    // $scope.load_data    = function()
});

//绑定手机号
app.controller('BindUserName', function($scope, $http, $location, common, wechat){
    $scope.username     = '';
    $scope.verifyCode   = '';
    $scope.childLevel   = 1;
    $scope.banSendCode  = 0;

    //初始化JS
    $scope.init_js  = function(){
        common.setTitle('绑定手机号');
        wechat.wxConfig();
        
    }

    //发送验证码
    $scope.send_code    = function(){
        if(String($scope.username).length != 11){
            common.doAlert('手机号格式错误!');
            return false;
        }

        var url     = config.prefix_url + '/Public/sendVerifyCode?username='+$scope.username;
        $http.get(url).success(function(info){
            console.log(info);
            if(info.code != 1){
                common.dealError(info);
                return false;
            }

            $scope.banSendCode    = 60;
            $scope.ban_send();
        })
    }

    //禁止发送
    $scope.ban_send     = function(){
        setTimeout(function(){
            if($scope.banSendCode == 0){
                return false;
            }
            $scope.$apply(function(){
                $scope.banSendCode--;
            })
            $scope.ban_send();
        }, 1000)
    }

    //绑定手机号
    $scope.submit    = function(){
        if(String($scope.verifyCode).length != 6){
            common.doAlert('验证码格式不正确!');
            return false;
        }

        var url     = config.prefix_url + '/User/bindUsername?code='+config.code;
        var param   = {
            'username'  : $scope.username,
            'verify_code'   : $scope.verifyCode,
            'child_level'   : $scope.childLevel
        }
        
        $http.post(url, param).success(function(info){
            if(info.code != 1){
                common.dealError(info);
                return false;
            }
            common.backAnchor();
        })
    }

    $scope.init_js();
})